﻿using LServer.Model.Logic.Entitys;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace LServer.Model.Managers
{
    public class ChannelConfig
    {
        public static Dictionary<string,ChannelEntity> m_ChannelDic;

        public static void Init() 
        {
            m_ChannelDic = new Dictionary<string,ChannelEntity>();
            string path = AppDomain.CurrentDomain.BaseDirectory + "Configs\\ChannelConfig.xml"; 

            XDocument doc = XDocument.Load(path);
            IEnumerable<XElement> enumerable = doc.Descendants("Channel");
            foreach (XElement item in enumerable) 
            {
                ChannelEntity channelEntity = new ChannelEntity();
                int.TryParse(item.Attribute("ChannelId").Value, out channelEntity.ChannelId);
                int.TryParse(item.Attribute("InnerVersion").Value, out channelEntity.InnerVersion);
                channelEntity.SourceVersion = item.Attribute("SourceVersion").Value;
                channelEntity.SourceUrl = item.Attribute("SourceUrl").Value;
                channelEntity.RechargeUrl = item.Attribute("RechargeUrl").Value;
                int.TryParse(item.Attribute("TDAppId").Value, out channelEntity.TDAppId);
                bool.TryParse(item.Attribute("IsOpenTD").Value, out channelEntity.IsOpenTD);

                m_ChannelDic[channelEntity.ChannelId + "_" + channelEntity.InnerVersion] = channelEntity;
            }

            Console.WriteLine("ChannelConfig Init Complete");
        }

        public static ChannelEntity Get(string channelId,string innerVersion)
        {
            ChannelEntity channelEntity;
            m_ChannelDic.TryGetValue(channelId + "_" + innerVersion, out channelEntity);
            return channelEntity;
        }
    }
}
